package com.mdc.mansion.core.modules.pay.impl;
import com.mdc.mansion.core.common.util.RestaurantSupport;
import com.mdc.mansion.core.modules.pay.dto.RestaurantPayCfgDto;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.modules.restaurant.entity.Restaurant;
import com.mdc.mansion.dataaccess.modules.system.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mdc.mansion.dataaccess.common.service.CrudService;

import com.mdc.mansion.dataaccess.modules.pay.dao.RestaurantPayCfgDao;
import com.mdc.mansion.dataaccess.modules.pay.entity.RestaurantPayCfg;

import com.mdc.mansion.core.modules.pay.RestaurantPayCfgService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 在线支付配置ServiceImpl
 * @author xizf
 * @version 2018-08-20
 */
@Service
public class RestaurantPayCfgServiceImpl extends CrudService<RestaurantPayCfgDao, RestaurantPayCfg> implements RestaurantPayCfgService {
	
	@Autowired
	private  RestaurantPayCfgDao restaurantPayCfgDao;
	
	/**
	 * 获取单个餐厅的配置信息
	 * @return
	 */
	@Override
	public RestaurantPayCfgDto getPayCfgInfo() {
		Restaurant restaurant = ContextHolder.currentRestaurant();
		RestaurantPayCfgDto dto = new RestaurantPayCfgDto();
		RestaurantPayCfg cfg = new RestaurantPayCfg();
		cfg.setRestaurantId(restaurant.getId());
		cfg = this.get(cfg);
		BeanUtils.copyProperties(cfg,dto);
		return dto;
	}
	
	@Transactional
	@Override
	public void savePayInfo(RestaurantPayCfgDto dto) {
		if(StringUtils.isNotBlank(dto.getId()))
		{
			///更新
			this.updateCfgInfo(dto);
		}else{
			////新增
			this.newCfgInfo(dto);
		}
	}
	
	@Transactional
	protected void newCfgInfo(RestaurantPayCfgDto dto) {
		RestaurantPayCfgDto cfg = new RestaurantPayCfgDto();
		BeanUtils.copyProperties(dto,cfg);
		User currentOperationUser = RestaurantSupport.getCurrentOperationUser();
		cfg.setCreateBy(currentOperationUser);
		cfg.setUpdateBy(currentOperationUser);
		this.savePayInfo(cfg);
	}
	
	@Transactional
	protected void updateCfgInfo(RestaurantPayCfgDto dto) {
		RestaurantPayCfg cfg = new RestaurantPayCfg();
		BeanUtils.copyProperties(dto,cfg);
		cfg.setRestaurantId(ContextHolder.currentRestaurantId());
		User currentOperationUser = RestaurantSupport.getCurrentOperationUser();
		cfg.setCreateBy(currentOperationUser);
		cfg.setUpdateBy(currentOperationUser);
		this.update(cfg);
	}
	
	
}
